// scaup - Scala up!
// (c) 2008-2010, Normen Müller, normen.mueller@gmail.com  
// $Id: shells.scala 47 2010-01-12 20:22:35Z normen.mueller $
package scaup.io

/** Provides console color access.
 * 
 * @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 * @version 0.0.3
 * @see http://linux.about.com/library/cmd/blcmdl4_console_codes.htm */
trait Stainable {  
    /** Set console foreground color.
     * @param  color Set this color. */
    def setForegroundColor(color: ShellForegroundColor.Value)
    
    /** Set console background color.
     * @param color Set this color. */
    def setBackgroundColor(color: ShellBackgroundColor.Value)
    
    /** Reset console backgound and foreground colors to initial values. */
    def resetColors()
}

/** Common foreground colors.
 * 
 * @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 * @version 0.0.3 */
object ShellForegroundColor extends Enumeration {
  val BLACK,DARK_RED, DARK_GREEN, DARK_YELLOW, DARK_BLUE, DARK_MAGENTA, DARK_CYAN, GREY,
  LIGHT_RED, LIGHT_GREEN, LIGHT_YELLOW, LIGHT_BLUE, LIGHT_MAGENTA, LIGHT_CYAN, WHITE = Value
}

/** Common background colors.
 * 
 * @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 * @version 0.0.3 */
object ShellBackgroundColor extends Enumeration {
  val BLACK, DARK_RED, DARK_GREEN, DARK_YELLOW, DARK_BLUE, DARK_MAGENTA, DARK_CYAN, GREY = Value
}

/** Unix console colors with ESC sequences.
 * 
 * @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 * @version 0.0.3*/
private[io] final class UnixConsole extends Stainable {  
  /** Resets the color to default. */
  val RESET = "\u001B[0m"
  
  /** Black foreground. */
  val BLACK = "\u001B[22;30m"
  
  /** Normal intensity red. */
  val RED = "\u001B[22;31m"
  
  /** Normal intensity green. */
  val GREEN = "\u001B[22;32m"
  
  /** Normal intesity yellow (brown). */
  val YELLOW = "\u001B[22;33m"
  
  /** Normal intensity blue. */
  val BLUE = "\u001B[22;34m" 
  
  /** Normal intensity magenta (purple). */
  val MAGENTA = "\u001B[22;35m"
  
  /** Normal intensity cyan. */
  val CYAN = "\u001B[22;36m"
  
  /** Normal intesity white (grey). */
  val WHITE = "\u001B[22;37m"
  
  /** Bright black (grey). */
  val GREY = "\u001B[1;30m"
  
  /** Bright red. */
  val LIGHT_RED = "\u001B[1;31m"
  
  /** Bright green. */
  val LIGHT_GREEN = "\u001B[1;32m"
  
  /** Bright yellow. */
  val LIGHT_YELLOW = "\u001B[1;33m"
  
  /** Bright blue. */
  val LIGHT_BLUE = "\u001B[1;34m"
  
  /** Bright magenta. */
  val LIGHT_MAGENTA = "\u001B[1;35m"
  
  /** Bright cyan. */
  val LIGHT_CYAN = "\u001B[1;36m"
  
  /** Bright white (white). */
  val LIGHT_WHITE = "\u001B[1;37m"
  
  /** Black background. */
  val BACKGROUND_BLACK = "\u001B[40m"
  
  /** Red background. */
  val BACKGROUND_RED = "\u001B[41m"
  
  /** Green background. */
  val BACKGROUND_GREEN = "\u001B[42m"
  
  /** Yellow background. */
  val BACKGROUND_YELLOW = "\u001B[43m"
  
  /** Blue background. */
  val BACKGROUND_BLUE = "\u001B[44m"
  
  /** Magenta background. */
  val BACKGROUND_MAGENTA = "\u001B[45m"
  
  /** Cyan background. */
  val BACKGROUND_CYAN = "\u001B[46m"
  
  /** White background (grey). */
  val BACKGROUND_WHITE = "\u001B[47m"
  
  /** Map of foreground colors. */
  private val fcolors = Map(
    ShellForegroundColor.BLACK -> BLACK,
    ShellForegroundColor.DARK_RED -> RED,
    ShellForegroundColor.DARK_GREEN -> GREEN,
    ShellForegroundColor.DARK_YELLOW -> YELLOW,
    ShellForegroundColor.DARK_BLUE -> BLUE,
    ShellForegroundColor.DARK_MAGENTA -> MAGENTA,
    ShellForegroundColor.DARK_CYAN -> CYAN,
    ShellForegroundColor.GREY -> WHITE,
    ShellForegroundColor.LIGHT_RED -> LIGHT_RED,
    ShellForegroundColor.LIGHT_GREEN -> LIGHT_GREEN,
    ShellForegroundColor.LIGHT_YELLOW -> LIGHT_YELLOW,
    ShellForegroundColor.LIGHT_BLUE -> LIGHT_BLUE,
    ShellForegroundColor.LIGHT_MAGENTA -> LIGHT_MAGENTA,
    ShellForegroundColor.LIGHT_CYAN -> LIGHT_CYAN,
    ShellForegroundColor.WHITE -> LIGHT_WHITE
  )    
  
  /** Map of background colors. */
  private val bcolors = Map(
    ShellBackgroundColor.BLACK -> BACKGROUND_BLACK,
    ShellBackgroundColor.DARK_RED -> BACKGROUND_RED,
    ShellBackgroundColor.DARK_GREEN -> BACKGROUND_GREEN,
    ShellBackgroundColor.DARK_YELLOW -> BACKGROUND_YELLOW,
    ShellBackgroundColor.DARK_BLUE -> BACKGROUND_BLUE,
    ShellBackgroundColor.DARK_MAGENTA -> BACKGROUND_MAGENTA,
    ShellBackgroundColor.DARK_CYAN -> BACKGROUND_CYAN,
    ShellBackgroundColor.GREY -> BACKGROUND_WHITE
  )
  
  def setForegroundColor(color: ShellForegroundColor.Value) = print(fcolors(color))
  
  def setBackgroundColor(color: ShellBackgroundColor.Value) = print(bcolors(color))
  
  def resetColors() = print(RESET)    	
}
